from dao.item import ItemDao
from lib.validator import Validator
 
class ItemService():
    def __init__(self, name):
        """Arguments:
            name -- string type
            item_id -- BSON ObjectId type
        """
        self.name = name

class ItemValidator(Validator):
    def __init__(self):
        Validator.__init__(self)
              
        self.rules = {'name': { 
                                'required' : [None, True],
                                'maxlength':[None, 32],
                                'unique' : [self.unique, 'name', 'The name exists, please try another']
                                },
                      'price':{'numeric' : [None, True]
                               }
        }

    def unique(self, val, field_name):
        _dao = ItemDao()
        ret = _dao.find_one({field_name:val})
        return ret is None 